package leetcode.d1;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: hang an
 * @Date: 2025/4/20
 * @Description:
 */
public class R781 {
    public int numRabbits(int[] answers) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int answer : answers) {
            map.put(answer, map.getOrDefault(answer, 0) + 1);
        }
        int res = 0;
        Set<Map.Entry<Integer, Integer>> entries = map.entrySet();
        for (Map.Entry<Integer, Integer> entry : entries) {
            int key = entry.getKey();
            int value = entry.getValue();
            if (key == 0) {
                res += value;
            } else {
                // 配对所需个数
                int maxH = key + 1;
                // 配对组数
                int group = value / maxH;
                if (value % maxH != 0) {
                    group++;
                }
                res += group * maxH;
            }
        }
        return res;
    }

}
